package hu.monitor.remoting;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

public class InvocationContext {
	private final Map<String, Object> attributes = new HashMap<String, Object>();
	private final Class<?> remoteInterface;
	private final Method method;
	private final Object[] args;

	private Iterator<? extends InvokerInterceptor> interceptors;

	public InvocationContext(final Class<?> remoteInterface, final Method method, final Object[] args) {
		this.remoteInterface = remoteInterface;
		this.method = method;
		this.args = args;
	}

	void setInterceptorIterator(final Iterator<? extends InvokerInterceptor> iterator) {
		this.interceptors = iterator;
	}

	public Class<?> getRemoteInterface() {
		return this.remoteInterface;
	}

	public Method getMethod() {
		return this.method;
	}

	public Object[] getArgs() {
		return this.args;
	}

	public Map<String, Object> getAttributes() {
		return this.attributes;
	}

	public Object invoke() throws Throwable {
		if (this.interceptors.hasNext()) {
			final InvokerInterceptor interceptor = this.interceptors.next();
			return interceptor.intercept(this);
		}

		throw new IllegalStateException("No more interceptors");
	}

}
